package com.interrobang.gui;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.interrobang.anime.Anime;
import com.interrobang.anime.AnimeList;
import com.interrobang.anime.NameChecker;

public class GamePanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8513408463723895851L;
	JButton roll;
	AnimeList list;
	private final String FILENAME = "Data/data.haruhi";
	Random rand;
	ButtonListener listener;
	Anime anime;
	private MainPanel panel;
	JButton back;
	private ImagePanel image;
	private JScrollPane listPane;
	private JList jList;
	private JLabel title;
	private JLabel episode;

	public GamePanel(MainPanel panel) {
		this.panel = panel;
		rand = new Random();
		listener = new ButtonListener();
		FileInputStream fis = null;
		ObjectInputStream in = null;
		try {
			fis = new FileInputStream(FILENAME);
			in = new ObjectInputStream(fis);
			list = (AnimeList) in.readObject();
			in.close();
		} catch (IOException e) {
			list = new AnimeList();
		} catch (ClassNotFoundException e) {
			// do something here
		}
				update();
	}

	private void initComponents() {

        image = new com.interrobang.gui.ImagePanel();
        listPane = new javax.swing.JScrollPane();
        jList = new javax.swing.JList();
        title = new javax.swing.JLabel();
        roll = new javax.swing.JButton();
        back = new javax.swing.JButton();
        episode = new javax.swing.JLabel();

        javax.swing.GroupLayout imageLayout = new javax.swing.GroupLayout(image);
        image.setLayout(imageLayout);
        imageLayout.setHorizontalGroup(
            imageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 285, Short.MAX_VALUE)
        );
        imageLayout.setVerticalGroup(
            imageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 214, Short.MAX_VALUE)
        );

        if(anime != null){
        	String[] entries;
    		entries = new String[anime.getRules().size()];
    			for (int i = 0; i < entries.length; i++) 
    			{
    				entries[i] = anime.getRules().get(i).getRule();
    			}
    			try {
    				NameChecker check = new NameChecker();
					BufferedImage im = ImageIO.read(new File("Graphics/" + check.check(anime.getTitle()) + ".png"));
    				 image.setImage(im);
    			} catch (IOException e) {
    				//do nothing maybe add a default image later
    			}
        

            jList = new JList(entries);
        listPane.setViewportView(jList);

        title.setFont(new java.awt.Font("Comic Sans MS", 0, 24)); // NOI18N
        title.setText(anime.getTitle());
        title.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        roll.setFont(new java.awt.Font("Comic Sans MS", 0, 12)); // NOI18N
        roll.setText("Roll");
        

        back.setFont(new java.awt.Font("Comic Sans MS", 0, 12)); // NOI18N
        back.setText("Back to Menu");

        episode.setFont(new java.awt.Font("Comic Sans MS", 0, 24)); // NOI18N
         episode.setText("Episode: " + rollEpisode());
        }
        else
        {
        	String[] entries;
    		entries = new String[0];
    			for (int i = 0; i < entries.length; i++) 
    			{
    				entries[i] = "";
    			}
    			
        

            jList = new JList(entries);
        listPane.setViewportView(jList);

        title.setFont(new java.awt.Font("Comic Sans MS", 0, 24)); // NOI18N
        title.setText("");
        title.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        roll.setFont(new java.awt.Font("Comic Sans MS", 0, 12)); // NOI18N
        roll.setText("Roll");
        

        back.setFont(new java.awt.Font("Comic Sans MS", 0, 12)); // NOI18N
        back.setText("Back to Menu");

        episode.setFont(new java.awt.Font("Comic Sans MS", 0, 24)); // NOI18N
         episode.setText("");
        }
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(72, 72, 72)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(listPane, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(image, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(65, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(147, 147, 147)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(roll, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                    .addComponent(back, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(411, 411, 411))
            .addGroup(layout.createSequentialGroup()
                .addGap(154, 154, 154)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(episode)
                    .addComponent(title))
                .addContainerGap(144, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(title)
                .addGap(18, 18, 18)
                .addComponent(episode)
                .addGap(3, 3, 3)
                .addComponent(image, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(listPane, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(roll)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(back)
                .addContainerGap())
        );
        roll.addActionListener(listener);
        back.addActionListener(listener);
    }
	
	public void update() {
		FileInputStream fis = null;
		ObjectInputStream in = null;
		try {
			fis = new FileInputStream(FILENAME);
			in = new ObjectInputStream(fis);
			list = (AnimeList) in.readObject();
			in.close();
		} catch (IOException e) {
			list = new AnimeList();
		} catch (ClassNotFoundException e) {
			// do something here
		}
		removeAll();
		
		initComponents();
		updateUI();
	}

	public Anime roll() {
		return list.get(rand.nextInt(list.size()));
	}

	public int rollEpisode() {
		return (rand.nextInt(anime.getEpisodes()) + 1);
	}

	private class ButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent event) {
			if (event.getSource() == roll) {
				if (list.size() == 0) {
					JOptionPane.showMessageDialog(panel,
							"There are currently no animes in the System");
				} else {
					anime = roll();
					update();
				}
			}
			if (event.getSource() == back) {
				panel.setViewActive(false);
				panel.setGameActive(false);
				panel.setMenuActive(true);
				panel.update();
			}

		}

	}
}
